package com.banhujiu.lakalaPlugin.utils;


import com.banhujiu.lakalaPlugin.exceptions.LklApiException;

import java.io.UnsupportedEncodingException;
import java.util.StringTokenizer;

/**
 * 查询支付记录 加密解密算法
 * User: Loki_Yung
 * Date:2016-12-8 16:05
 */
public class CryptUtil {
	/**
	 * 字符串加密
	 *
	 * @param ssoToken 字符串
	 *
	 * @return String 返回加密字符串
	 */
	public static String encrypt(String ssoToken) {
		byte[] _ssoToken;
		try {
			_ssoToken = ssoToken.getBytes("ISO-8859-1");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			throw new LklApiException("字符串转换失败，不支持的编码");
		}
		StringBuilder name = new StringBuilder();
		for (int i = 0; i < _ssoToken.length; i++) {
			int asc = _ssoToken[i];
			_ssoToken[i] = (byte) (asc + 27);
			name.append(asc + 27).append("%");
		}
		return name.toString();
	}

	/**
	 * 字符串解密
	 *
	 * @param ssoToken 字符串
	 *
	 * @return String 返回加密字符串
	 */
	public String decrypt(String ssoToken) {
		StringBuilder name = new StringBuilder();
		StringTokenizer st = new StringTokenizer(ssoToken, "%");
		while (st.hasMoreElements()) {
			int asc = Integer.parseInt((String) st.nextElement()) - 27;
			name.append((char) asc);
		}
		return name.toString();
	}

	public static void main(String[] args) {
		String ss = "150%61%143%141%124%137%142%124%126%143%132%138%137%122%132%127%61%85%61%76%81%76%77%77%75%77%75%75%78%77%83%83%81%61%71%61%141%128%129%128%141%137%144%136%125%128%141%61%85%61%76%77%77%75%77%75%75%78%77%83%83%81%61%71%61%139%124%148%122%124%136%143%61%85%61%75%75%75%75%75%75%75%79%80%80%84%83%83%75%75%61%71%61%138%141%127%128%141%132%127%122%142%126%124%137%61%85%61%59%61%71%61%125%124%137%134%122%143%148%139%128%61%85%61%75%76%75%80%75%75%75%76%61%71%61%138%139%128%137%132%127%61%85%61%61%71%61%142%148%142%143%141%124%126%128%137%138%61%85%61%75%75%75%75%75%82%61%71%61%136%126%131%122%132%127%61%85%61%83%77%77%76%75%75%75%80%75%76%78%75%83%78%80%61%71%61%127%128%145%132%126%128%122%132%137%129%138%61%85%61%84%76%82%76%82%80%80%84%61%71%61%138%144%143%122%143%141%124%127%128%122%137%138%61%85%61%59%61%71%61%126%124%141%127%122%137%138%61%85%61%81%77%76%79%84%84%69%69%69%69%69%69%75%81%84%79%61%71%61%125%124%143%126%131%125%132%135%135%137%138%61%85%61%75%75%75%76%78%77%61%71%61%126%138%144%139%138%137%122%129%128%128%61%85%61%59%61%71%61%143%138%143%124%135%122%129%128%128%61%85%61%75%75%75%75%75%75%75%79%80%80%84%83%83%75%75%61%71%61%143%141%124%127%128%122%143%148%139%128%61%85%61%75%76%77%75%75%76%61%71%61%141%128%142%144%135%143%122%126%138%127%128%61%85%61%110%112%94%94%96%110%110%61%71%61%139%124%148%122%143%148%139%128%61%85%61%75%75%61%71%61%143%132%136%128%122%128%137%127%61%85%61%77%75%76%81%76%77%77%75%76%75%77%75%80%75%61%152%";
		CryptUtil md5Util = new CryptUtil();
		String dd = md5Util.decrypt(ss);
		System.out.println(dd + '\n');
		System.out.println(CryptUtil.encrypt(dd));
	}
}
